package cn.laowen.system.service.impl;

import cn.laowen.system.domain.Role;
import cn.laowen.system.mapper.RoleMapper;
import cn.laowen.system.service.IRoleService;
import cn.laowen.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laowen
 * @since 2022-06-21
 */
@Service
public class RoleServiceImpl extends BasicServiceImpl<Role> implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        //保存t_role表数据
        roleMapper.save(role);
        //保存中间表数据t_role_menu 和 t_role_permission
        initRelationTable(role);
    }

    /**
     * 保存到中间表数据 t_role_menu 和 t_role_permission
     * @param role
     */
    private void initRelationTable(Role role) {
        //清空中间表数据的历史数据
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());

        //保存到中间表数据 t_role_menu 和 t_role_permission
        roleMapper.saveRoleMenu(role);
        roleMapper.saveRolePermission(role);

    }


    @Override
    public void update(Role role) {
        roleMapper.update(role);
    }



    @Override
    public void removeById(Long id) {
        roleMapper.deleteById(id);
    }
}
